//
//  ViewController.m
//  GameSDK
//
//  Created by Johnny on 2017/7/25.
//  Copyright © 2017年 Johnny. All rights reserved.
//

#import "ViewController.h"
#import "GSDKComplatformBase.h"
#import <CommonCrypto/CommonDigest.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *goodsTableView;
@property (nonatomic, strong) NSArray * goods;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)login:(id)sender {
    [GSDKCOMPLATFORM login];
}

- (IBAction)logout:(id)sender {
    [GSDKCOMPLATFORM logout];
}

- (IBAction)switchAccount:(id)sender {
    [GSDKCOMPLATFORM switchAccount];
}

- (IBAction)registerAccount:(id)sender {
    [GSDKCOMPLATFORM registerAccount];
}

- (IBAction)saveServer:(id)sender {
    [GSDKCOMPLATFORM saveServerWithServerId:@"1" andServerName:@"测试服" andRoleId:@"1" andRoleName:@"测试账号" andRoleLevel:@"99"];
}

- (IBAction)getGoods:(id)sender {
    [GSDKCOMPLATFORM getGoods];
}

- (void)reloadAt:(NSArray*)list {
    _goods = list;
    [_goodsTableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_goods count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString  * reuseIdentify = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentify];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    GSDKGoods * model = _goods[indexPath.row];
    cell.textLabel.text = model.goods_name;
    cell.detailTextLabel.text = model.goods_price;
    return cell;
}

//点击内购
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss:SSS"];
    NSString *date =  [formatter stringFromDate:[NSDate date]];
    NSString *timeLocal = [[NSString alloc] initWithFormat:@"%@", date];
    NSDictionary * dic = @{@"a": @"1", @"b": @"2"};
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dic
                                                        options:0
                                                          error:nil];
    NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    [GSDKCOMPLATFORM buyWithGoods:_goods[indexPath.row] andOrderId:[self md5:timeLocal] andGameOther:myString andSandbox:true];
}

- (NSString *) md5:(NSString *) input {
    const char *cStr = [input UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    return  output;
}
@end
